//
//  LSZExchangeVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/3/28.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZExchangeVC.h"

@interface LSZExchangeVC ()
{
    UIView * viewBack;
    UIView * viewInformation;

    NSInteger clicksection;//提现的那个
    NSInteger clickRow;//提现的那个
}
@end

@implementation LSZExchangeVC
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    __weak typeof(self) weakSelf = self;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [LSZRequestResolutionDic initWithGetUserInformation:^(BOOL isOK, NSString * str, LSZPersonalInformationManager * manager) {
        if (isOK == YES) {
             LSZPersonalInformationMg * mg = manager.mutDataArray[0];
             weakSelf.fuYinValue = NullStringjudgment(NSStr(mg.credit));
        } 
    }];
}
-(void)getDataDic
{
    [LSZRequestResolutionDic getTheConvertibilityInformation:^(BOOL isOk, NSString * str, LSZGetExchangeInformationManager * manager) {
        if (isOk == YES) {
            self.exchangeInformationManager = manager;
            [self.collectionViewExchange reloadData];
        }else{
            [self setStringWarning:str];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"兑换提现";
    [self setUIExchange];
    [self getDataDic];
}
-(void)setUIExchange
{
    CGFloat width;
    if(ScreenWidth ==414){
        width = (ScreenWidth - 50-1) / 2;
    }else
    if(ScreenWidth ==375){
       width = (ScreenWidth - 40-1) / 2;
    }else{
       width = (ScreenWidth - 30-1) / 2;
    }
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 10;
    if(ScreenWidth ==414){
        flowLayout.sectionInset = UIEdgeInsetsMake(15, 15,15,15);
        flowLayout.itemSize = CGSizeMake(width,width+15+20);
    }else
    if(ScreenWidth ==375){
        flowLayout.sectionInset = UIEdgeInsetsMake(15, 15,15,15);
        flowLayout.itemSize = CGSizeMake(width,width+15+20);
    }else{
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10,10,10);
        flowLayout.itemSize = CGSizeMake(width,width+15+30);
    }
    self.collectionViewExchange = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight-HOME_INDICATOR_HEIGHT) collectionViewLayout:flowLayout];
    self.collectionViewExchange.delegate   = self;
    self.collectionViewExchange.dataSource = self;
    self.collectionViewExchange.backgroundColor = RGBColor(236, 236, 236);
    [self.view addSubview:self.collectionViewExchange];
    [self.collectionViewExchange registerClass:[LSZExchangeCollectionViewCell class] forCellWithReuseIdentifier:@"LSZExchangeCollectionViewCell"];
    [self.collectionViewExchange registerClass:[LSZClassificationWithdrawalCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LSZClassificationWithdrawalCollectionReusableView"];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"我的订单" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if(ScreenWidth ==414){
        if (section == 0) {
            return CGSizeMake(ScreenWidth,40);
        }
        return CGSizeMake(ScreenWidth,35);
    }else
     if(ScreenWidth ==375){
         if (section == 0) {
             return CGSizeMake(ScreenWidth,35);
         }
         return CGSizeMake(ScreenWidth,35);
    }else{
        if (section == 0) {
            return CGSizeMake(ScreenWidth,30);
        }
        return CGSizeMake(ScreenWidth,25);
    }
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.exchangeInformationManager.arrayData.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    LSZGetExchangeInformationMg * mg = nil;
    mg = self.exchangeInformationManager.arrayData[section];
    return mg.mutArrayMoney.count;
}
#pragma mark - 区头
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    LSZClassificationWithdrawalCollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LSZClassificationWithdrawalCollectionReusableView" forIndexPath:indexPath];
    LSZGetExchangeInformationMg * mg = nil;
    mg = self.exchangeInformationManager.arrayData[indexPath.section];
    headerView.labelTitle.text = [NSString stringWithFormat:@"%@提现",mg.name];
    if (indexPath.section == 0) {
        if(ScreenWidth ==414){
            headerView.labelTitle.frame = CGRectMake(15, 20,ScreenWidth, 20);
        }else
        if(ScreenWidth ==375){
            headerView.labelTitle.frame = CGRectMake(15, 20,ScreenWidth, 20);
        }else{
            headerView.labelTitle.frame = CGRectMake(10,15,ScreenWidth, 20);
        }
    }else{
        if(ScreenWidth ==414){
            headerView.labelTitle.frame = CGRectMake(15, 10,ScreenWidth,20);
        }else
        if(ScreenWidth ==375){
            headerView.labelTitle.frame = CGRectMake(15, 10,ScreenWidth,20);
        }else{
            headerView.labelTitle.frame = CGRectMake(10,5,ScreenWidth, 20);
        }
    }
    return headerView;
}
#pragma mark - 单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LSZExchangeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LSZExchangeCollectionViewCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    LSZGetExchangeInformationMg * mg = nil;
    mg = self.exchangeInformationManager.arrayData[indexPath.section];
    cell.imageBigType.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",mg.name]];
    NSString * strTitle = [NSString stringWithFormat:@"%@提现%@元",mg.name,mg.mutArrayMoney[indexPath.row]];
    cell.labelMoney.text = strTitle;

    NSString * strMoney = [NSString stringWithFormat:@"%@元",mg.mutArrayMoney[indexPath.row]];
    cell.imageBk.image = [UIImage imageNamed:strMoney];

    NSString * str = [NSString stringWithFormat:@"%@红包%@元",mg.name,mg.mutArrayMoney[indexPath.row]];
    cell.labelMoney.text = str;
    NSInteger value = [mg.mutArrayMoney[indexPath.row] integerValue]* [mg.proportion integerValue];
    NSString * strMoneyValue = [NSString stringWithFormat:@"提现消耗%ld福音值",(long)value];
    cell.labelIsOrOther.text = strMoneyValue;
    [cell.layer setMasksToBounds:YES];
    [cell.layer setCornerRadius:4];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LSZGetExchangeInformationMg * mg = nil;
    mg = self.exchangeInformationManager.arrayData[indexPath.section];
    NSInteger  value = [mg.mutArrayMoney[indexPath.row] integerValue]* [mg.proportion integerValue];
    if ([self.fuYinValue intValue]< value) {
        [self setStringWarning:@"您的福音值不足,不能兑换!"];
        return;
    }else{
        clicksection = indexPath.section;
        clickRow = indexPath.row;
        [self promptInformation];
    }
}
-(void)promptInformation
{
    viewBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    viewBack.backgroundColor = [UIColor blackColor];
    viewBack.alpha = 0.2;
    [self.view addSubview:viewBack];
    [viewBack tapGesture:^(UIGestureRecognizer *ges) {
        [self geToCancel];
    }];

    viewInformation =[[UIView alloc]init];
    viewInformation.backgroundColor = [UIColor whiteColor];
    [viewInformation.layer setMasksToBounds:YES];
    [viewInformation.layer setCornerRadius:4];
    [self.view addSubview:viewInformation];

    UILabel * label = [[UILabel alloc]init];
    label.textColor = titleColorLSZ;
    label.text = @"温馨提示";
    [viewInformation addSubview:label];

    UILabel * labelOne =[[UILabel alloc]init];
    labelOne.textColor = titleColorLSZ;
    labelOne.text = @"提现请先添加客服微信";
    [viewInformation addSubview:labelOne];

    UILabel * labelTwo =[[UILabel alloc]init];
    labelTwo.textColor = redUIColor;
    labelTwo.numberOfLines = 0;
    labelTwo.text = @"点击复制客服微信号:shengjing2019";
    [viewInformation addSubview:labelTwo];
    [labelTwo tapGesture:^(UIGestureRecognizer *ges) {
        [self clickWeiXin];
    }];
    UILabel * labelThree = [[UILabel alloc]init];
    labelThree.textColor = redUIColor;
    labelThree.text = @"去提现";
    labelThree.textAlignment = NSTextAlignmentRight;
    [viewInformation addSubview:labelThree];

    [labelThree tapGesture:^(UIGestureRecognizer *ges) {
        [self geToMoney];
    }];
    if (ScreenWidth == 320) {
        //5
        viewInformation.frame = CGRectMake(20,(ScreenHeight-100)/2, ScreenWidth-40, 100+10);
        label.font = FONT_(15);
        label.frame = CGRectMake(10, 10, viewInformation.frame.size.width, 18);
        labelOne.font = FONT_(13.5);
        labelOne.frame = CGRectMake(10, CGRectGetMaxY(label.frame)+5, viewInformation.frame.size.width-20, 16);
        labelTwo.font = FONT_(13.5);
        labelTwo.frame = CGRectMake(10, CGRectGetMaxY(labelOne.frame)+2, viewInformation.frame.size.width-20, 20);
        labelThree.font = FONT_(14);
        labelThree.frame = CGRectMake(10, viewInformation.frame.size.height-10-16, viewInformation.frame.size.width-20, 18);
    }else if(ScreenWidth == 375){
        // 6 7
        viewInformation.frame = CGRectMake(40,(ScreenHeight-110)/2, ScreenWidth-80, 110+10);
        label.font = FONT_(15.5);
        label.frame = CGRectMake(10, 10, viewInformation.frame.size.width, 20);

        labelOne.font = FONT_(14);
        labelOne.frame = CGRectMake(10, CGRectGetMaxY(label.frame)+5, viewInformation.frame.size.width-20, 18);
        labelTwo.font = FONT_(14);
        labelTwo.frame = CGRectMake(10, CGRectGetMaxY(labelOne.frame)+2, viewInformation.frame.size.width-20, 22);
        labelThree.font = FONT_(14.5);
        labelThree.frame = CGRectMake(10, viewInformation.frame.size.height-10-20, viewInformation.frame.size.width-20, 20);
    }else {
        //plus
        viewInformation.frame = CGRectMake(45,(ScreenHeight-118)/2, ScreenWidth-90, 118+10);
        label.font = FONT_(16);
        label.frame = CGRectMake(10, 10, viewInformation.frame.size.width, 22);

        labelOne.font = FONT_(14.5);
        labelOne.frame = CGRectMake(10, CGRectGetMaxY(label.frame)+5, viewInformation.frame.size.width-20, 20);
        labelTwo.font = FONT_(14.5);
        labelTwo.frame = CGRectMake(10, CGRectGetMaxY(labelOne.frame)+2, viewInformation.frame.size.width-20, 24);
        labelThree.font = FONT_(15);
        labelThree.frame = CGRectMake(10, viewInformation.frame.size.height-10-22, viewInformation.frame.size.width-20, 22);
    }
}
-(void)geToMoney
{
    [viewBack removeFromSuperview];
    [viewInformation removeFromSuperview];
    
    LSZGetExchangeInformationMg * mg = nil;
    mg = self.exchangeInformationManager.arrayData[clicksection];
    LSZShowDetailsViewController * vc = [[LSZShowDetailsViewController alloc]init];
    vc.typeStr      = mg.id_id;
    vc.moneyStr     =  mg.mutArrayMoney[clickRow];
    vc.promptString = [NSString stringWithFormat:@"温馨提示:%@",mg.introduce];
    vc.nameString   = mg.name;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)geToCancel
{
    [viewBack removeFromSuperview];
    [viewInformation removeFromSuperview];
}
-(void)clickWeiXin
{
    UIPasteboard * pastboard = [UIPasteboard generalPasteboard];
    pastboard.string = @"shengjing2019";
    [self setStringWarning:@"已复制到剪切板!"];
}
#pragma mark - 订单界面
-(void)onClickedOKbtn
{
    LSZWithdrawalVC * vc =[[LSZWithdrawalVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
